package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    MemberDao memberDao;
    @Autowired
    SetmealDao setmealDao;
    @Autowired
    OrderDao orderDao;

    /**
     * 获得运营统计数据
     * Map数据格式：
     * todayNewMember -> number
     * totalMember -> number
     * thisWeekNewMember -> number
     * thisMonthNewMember -> number
     * todayOrderNumber -> number
     * todayVisitsNumber -> number
     * thisWeekOrderNumber -> number
     * thisWeekVisitsNumber -> number
     * thisMonthOrderNumber -> number
     * thisMonthVisitsNumber -> number
     * hotSetmeal -> List<Setmeal>
     */
    @Override
    public Map<String, Object> getBusinessReport() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int totalMember = memberDao.findMemberTotalCount();
        Date date = new Date();
        String today = sdf.format(date);
        String week = sdf.format(DateUtils.getThisWeekMonday());
        String month = sdf.format(DateUtils.getFirstDay4ThisMonth());
        int todayNewMember = memberDao.findMemberCountByDate(today);
        int thisWeekNewMember = memberDao.findMemberCountByDate(week);
        int thisMonthNewMember = memberDao.findMemberCountByDate(month);
        int todayOrderNumber = orderDao.findOrderCountByDate(today);
        int thisWeekOrderNumber = orderDao.findOrderCountByDate(week);
        int thisMonthOrderNumber = orderDao.findOrderCountByDate(month);
        int todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        int thisWeekVisitsNumber = orderDao.findVisitsCountByDate(week);
        int thisMonthVisitsNumber = orderDao.findVisitsCountByDate(month);
        //热门套餐（取前5）
        List<Map> hotSetmeal = setmealDao.findHotSetmeal();
        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", today);
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);
        return result;
    }
}
